//
//  CBPeripheralManager+Rx.swift
//  RXSwiftDemo1
//
//  Created by mac on 2025/2/28.
//

import RxSwift
import RxCocoa
import CoreBluetooth

public class RxCBPeripheralManagerDelegateProxy: DelegateProxy<CBPeripheralManager, CBPeripheralManagerDelegate>, DelegateProxyType {
    
    public init(peripheralManager: CBPeripheralManager) {
        super.init(parentObject: peripheralManager, delegateProxy: RxCBPeripheralManagerDelegateProxy.self)
    }
    
    public static func registerKnownImplementations() {
        self.register { RxCBPeripheralManagerDelegateProxy(peripheralManager: $0) }
    }
    
    public static func currentDelegate(for object: CBPeripheralManager) -> CBPeripheralManagerDelegate? {
        return object.delegate
    }
    
    public static func setCurrentDelegate(_ delegate: CBPeripheralManagerDelegate?, to object: CBPeripheralManager) {
        object.delegate = delegate
    }
}

extension Reactive where Base: CBPeripheralManager {
    public var delegate: DelegateProxy<CBPeripheralManager, CBPeripheralManagerDelegate> {
        return RxCBPeripheralManagerDelegateProxy.proxy(for: base)
    }
    
    // 外设管理器状态更新
    public var didUpdateState: Observable<CBManagerState> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManagerDidUpdateState(_:)))
            .compactMap { $0[0] as? CBManagerState }
    }
    
    // 开始广播
    public var didStartAdvertising: Observable<Error?> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManagerDidStartAdvertising(_:error:)))
            .compactMap { $0[1] as? Error }
    }
    
    // 添加服务
    public var didAddService: Observable<(CBService, Error?)> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:didAdd:error:)))
            .compactMap { ($0[1] , $0[2]) as? (CBService, (any Error)?) }
    }
    
    // 订阅特征
    public var didSubscribeToCharacteristic: Observable<(CBCentral, CBCharacteristic)> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:central:didSubscribeTo:)))
            .compactMap { ($0[1], $0[2]) as? (CBCentral, CBCharacteristic) }
    }
    
    // 取消订阅特征
    public var didUnsubscribeFromCharacteristic: Observable<(CBCentral, CBCharacteristic)> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:central:didUnsubscribeFrom:)))
            .compactMap { ($0[1], $0[2]) as? (CBCentral, CBCharacteristic) }
    }
    
    // 接收读请求
    public var didReceiveReadRequest: Observable<CBATTRequest> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:didReceiveRead:)))
            .compactMap { $0[1] as? CBATTRequest }
    }
    
    // 接收写请求
    public var didReceiveWriteRequests: Observable<[CBATTRequest]> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:didReceiveWrite:)))
            .compactMap { $0[1] as? [CBATTRequest] }
    }
    
    // 准备更新订阅者
    public var readyToUpdateSubscribers: Observable<Void> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManagerIsReady(toUpdateSubscribers:)))
            .map { _ in () }
    }
    
    // 发布 L2CAP 通道
    public var didPublishL2CAPChannel: Observable<(CBL2CAPPSM, Error?)> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:didPublishL2CAPChannel:error:)))
            .compactMap { ($0[1], $0[2]) as? (CBL2CAPPSM, (any Error)?) }
    }
    
    // 取消发布 L2CAP 通道
    public var didUnpublishL2CAPChannel: Observable<(CBL2CAPPSM, Error?)> {
        return delegate
            .methodInvoked(#selector(CBPeripheralManagerDelegate.peripheralManager(_:didUnpublishL2CAPChannel:error:)))
            .compactMap { ($0[1], $0[2]) as? (CBL2CAPPSM, (any Error)?) }
    }
}
